class Solution {
public:

    void swap(string& s, int left, int right)
    {
        char tmp = s[left];
        s[left] = s[right];
        s[right] = tmp;
    }

    void reverse(string& s, int left, int right)
    {
        while (left <= right)
        {
            swap(s, left, right);
            left++;
            right--;
        }
    }

    string reverseWords(string s) {
        int left = 0;
        int i = 0;
        for (i = 0; i <= s.size(); i++)
        {
            if (s[i] == ' ' || s[i] == '\0')
            {
                reverse(s, left, i - 1);
                left = i + 1;
            }
        }
        return s;
    }
};